package com.baosight.sczx.zm.cs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cs.domain.ZMCSSystemModule;

/**
 * <p>Title: ServiceZMCS02.java</p>
 * <p>Description: 系统模块管理</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-1-10 上午09:18:52  张小飞  Create
 *</pre>
 */
public class ServiceZMCS02 extends ServiceEPBase {

	/**系统模块：数据初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午09:19:33 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = null;
		
		String fdParentId = inInfo.getString("fdParentId");
		String isLeaf = inInfo.getString("isLeaf");
		inInfo.addBlock(EiConstant.queryBlock);
		
		//非叶子节点
		if(isLeaf != null && "0".equals(isLeaf)){
			inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
			outInfo = this.query(inInfo);
		}
		
		//叶子节点	
		else{
			inInfo.getBlock(EiConstant.queryBlock).setCell(0, "fdKeyId", fdParentId);//添加查询条件
			outInfo = super.query(inInfo, "ZMCSSystemModule.queryByKeyId", new ZMCSSystemModule());
		}
		
		return outInfo;
	}
	
	/**
	 * 系统模块叶子数据查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午09:19:33 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdParentId = inInfo.getString("fdParentId");
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCSSystemModule.query",new ZMCSSystemModule());
		return outInfo;
	}
	
	/**系统模块：叶子页面数据新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午09:19:33 张小飞
	 *    2011-3-17上午09:59:30 张小飞  修改编号和名称的校验规则
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验序号、编号、名称是否重复
		boolean isRepeat = validate(inInfo, "ZMCSSystemModule.validate", new HashMap<String, String>());
		
		if(isRepeat) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_SYSTEMMODEL"));//主键
			EiInfo outInfo = super.insert(inInfo, "ZMCSSystemModule.insert");
			return outInfo;
		}
	}
	
	/**系统模块：叶子页面数据修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午09:19:33 张小飞
	 *    2011-3-17上午09:59:30张小飞  修改编号和名称的校验规则
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验编号或名称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean isRepeat = validate(inInfo, "ZMCSSystemModule.validate", params);
		if(isRepeat) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCSSystemModule.update");
			return outInfo;
		}
	}
	
	/**
	 * 启用
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:12:10 严洪浩
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSSystemModule.modifyStart");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 停用
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:12:20 严洪浩
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSSystemModule.modifyStop");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**系统模块：树一级节点删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午09:19:33 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++){
			_list.add(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		params.put("fdKeyIds", _list);
		
		//有无子模块校验
		boolean validate = hasChildrenModule(inInfo, "ZMCSSystemModule.validateHasChildren", params);
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo _temp = super.delete(inInfo, "ZMCSSystemModule.delete");//执行删除
			outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;		
	}
	
	/**
	 * 判断模块下是否有子模块
	 * <p>Function: hasChildrenModule</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:12:31 严洪浩
	 * </p>
	 *
	 */
	private boolean hasChildrenModule(EiInfo inInfo, String sqlName, Map<String, List<String>> params) {
		List<?> result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("所选模块下有子模块，不能删除！");
			return true;
		}
		return false;
	}
	
	/**
	 * 序号、编号、名称重复验证
	 * <p>Function: validateNo</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:13:00 严洪浩
	 *    2011-3-17上午10:02:30 张小飞    本方法修改为单独对编号校验
	 *    2011-5-06下午20:44:00 严洪浩     修改验证方法，统一验证序号、编号、名称
	 * </p>
	 *
	 */
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdSerialNumber = inInfo.getBlock("result").getCellStr(0, "fdSerialNumber");
		String fdModuleNo = inInfo.getBlock("result").getCellStr(0, "fdModuleNo");
		String fdParentId = inInfo.getBlock("result").getCellStr(0, "fdParentId");
		String fdModuleName = inInfo.getBlock("result").getCellStr(0, "fdModuleName");
		
		params.put("fdSerialNumber", fdSerialNumber);
		params.put("fdModuleNo", fdModuleNo);
		params.put("fdParentId", fdParentId);
		params.put("fdModuleName", fdModuleName);
		
		List<?> result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSSystemModule _sysModule = (ZMCSSystemModule)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_sysModule.getFdSerialNumber() != null && _sysModule.getFdSerialNumber().toString().equals(fdSerialNumber)) {
				msg.append(" 序号 ");
			}
			if(_sysModule.getFdModuleNo() != null && _sysModule.getFdModuleNo().equals(fdModuleNo)) {
				msg.append(" 编号 ");
			}
			if(_sysModule.getFdModuleName() != null && _sysModule.getFdModuleName().equals(fdModuleName)) {
				msg.append(" 名称 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
}
